<?php

namespace IMooc;

class Controller
{
    protected array $data;
    protected string $controller_name;
    protected string $view_name;
    protected string $template_dir;

    function __construct($controller_name, $view_name)
    {
        $this->controller_name = $controller_name;
        $this->view_name = $view_name;
        $this->template_dir = Application::getInstance()->base_dir . '/templates';
    }

    function assign($key, $value): void
    {
        $this->data[$key] = $value;
    }

    function display($file = ''): void
    {
        if (empty($file)) {
            $file = strtolower($this->controller_name) . '/' . $this->view_name . '.php';
        }
        $path = $this->template_dir . '/' . $file;
        extract($this->data);
        include $path;
    }
}